package sk.gtug.hackatlon.drunkdroid;

import sk.gtug.hackatlon.drunkdroid.models.BeerManager;
import sk.gtug.hackatlon.drunkdroid.models.Collisional;
import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.os.Handler;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.Window;
import android.widget.RelativeLayout;

/**
 * @author Lubos Omelina
 */

public class Activity2Game extends Activity implements SensorEventListener{
	
	class Dude extends Collisional{
		private float mVerticalDirection=0;
		private float mHorizontalDirection=0;
		private int mAnim=0;
		private ImageView mImage;
		private int mOldX=100;
		private int mOldY=100;

		public Dude(int x, int y, int radius,ImageView img){
			super(x,y,radius);
			mImage = img;
		}

		void go(){
			mAnim++;
			if(mAnim > 7){
				mAnim = 0;
			}
			
			if(mOldY < this.getY()){
				mVerticalDirection = 1;
			} else {
				if(mOldY > this.getY()){
					mVerticalDirection = -1;
				}
			}
			mOldY = this.getY();
			
			if(mOldX < this.getX()){
				mHorizontalDirection = 1;
			} else {
				if(mOldX > this.getX()){
					mHorizontalDirection = -1;
				}
			}
			mOldX = this.getX();
			
			mImage.setImageResource(getAnim(mAnim));
		}
		private int getAnim(int anim){
			// ugly ugly ugly
			if(mVerticalDirection<0){
				if(mHorizontalDirection>0){
				switch(anim){
					case 0:
						return R.drawable.homer_walk_back001;
					case 1:
						return R.drawable.homer_walk_back002;
					case 2:
						return R.drawable.homer_walk_back003;
					case 3:
						return R.drawable.homer_walk_back004;
					case 4:
						return R.drawable.homer_walk_back005;
					case 5:
						return R.drawable.homer_walk_back006;
					case 6:
						return R.drawable.homer_walk_back007;
					case 7:
						return R.drawable.homer_walk_back008;
				}
				} else {
					switch(anim){
					case 0:
						return R.drawable.homer_walk_backl001;
					case 1:
						return R.drawable.homer_walk_backl002;
					case 2:
						return R.drawable.homer_walk_backl003;
					case 3:
						return R.drawable.homer_walk_backl004;
					case 4:
						return R.drawable.homer_walk_backl005;
					case 5:
						return R.drawable.homer_walk_backl006;
					case 6:
						return R.drawable.homer_walk_backl007;
					case 7:
						return R.drawable.homer_walk_backl008;
				}	
					
				}
			} else{
				if(mHorizontalDirection>0){
				switch(anim){
				case 0:
					return R.drawable.homer_walk_front001;
				case 1:
					return R.drawable.homer_walk_front002;
				case 2:
					return R.drawable.homer_walk_front003;
				case 3:
					return R.drawable.homer_walk_front004;
				case 4:
					return R.drawable.homer_walk_front005;
				case 5:
					return R.drawable.homer_walk_front006;
				case 6:
					return R.drawable.homer_walk_front007;
				case 7:
					return R.drawable.homer_walk_front008;
			}
				}else {
					switch(anim){
					case 0:
						return R.drawable.homer_walk_frontl001;
					case 1:
						return R.drawable.homer_walk_frontl002;
					case 2:
						return R.drawable.homer_walk_frontl003;
					case 3:
						return R.drawable.homer_walk_frontl004;
					case 4:
						return R.drawable.homer_walk_frontl005;
					case 5:
						return R.drawable.homer_walk_frontl006;
					case 6:
						return R.drawable.homer_walk_frontl007;
					case 7:
						return R.drawable.homer_walk_frontl008;
				}	
				}
				
			}
			return R.drawable.homer_walk_front001;
			
		}
	}
	
	private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private Display mDisplay;
    private Dude mDude;
    private int mWidth;
    private int mHeight;
    private static BeerManager mManager = null;
    
    private long mStartTime;
    private TextView mTextField;
    // This is a very funny thing. Try acceleration 10 or more and play the game :)
    // Invert mode: negative values (e.g. -5)
    private float mAcceleration = 5; // speed of moving
    private int mNumberOfBeers = 5; // number of generated beers
    
    
    
	private Handler reportHandler; 
	int x = 0;
	int beerLevel = 4;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(	WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                				WindowManager.LayoutParams.FLAG_FULLSCREEN); 
        setContentView(R.layout.screen2game);
        
        mDisplay = getWindowManager().getDefaultDisplay();
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mTextField = (TextView)findViewById(R.id.timeView);
        
        if(mManager == null){
        	mManager = new BeerManager();
        }
        
        
        mHeight = mDisplay.getHeight() - 48;
        mWidth = mDisplay.getWidth() - 48;
        
        mDude = new Dude(100, 100, 15,(ImageView)findViewById(R.id.dude));
        SetPosition(R.id.dude,mDude.getX()+23, mDude.getY()+33);
        setNumberOfBeers();
    }
    private void setNumberOfBeers(){
    	Bundle bundle = this.getIntent().getExtras();
        String gameMode = bundle.getString("mode");
        
        if(gameMode.equalsIgnoreCase("easy")) {
            mNumberOfBeers = 4;
            mAcceleration = 5;
        } else if(gameMode.equalsIgnoreCase("normal")) {
        	mNumberOfBeers = 5;
        	mAcceleration = 7;
        } else if(gameMode.equalsIgnoreCase("hard")) {
        	mNumberOfBeers = 6;
        	mAcceleration = 9;
        }
    }
    protected void onResume() {
        super.onResume();
        mManager.start(mNumberOfBeers,this);
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
        mStartTime = System.currentTimeMillis();
    }

    protected void onPause() {
        super.onPause();
        mManager.stop();
        mSensorManager.unregisterListener(this);
    }


	@Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
    }

	@Override
    public void onSensorChanged(SensorEvent arg0) {
		mDude.setX(	checkRange(0,
				mWidth,mDude.getX() - (int)( arg0.values[0] * mAcceleration )));
		mDude.setY(checkRange(0,
				mHeight,mDude.getY() + (int)( arg0.values[1] * mAcceleration )));
	    
	    SetPosition(R.id.dude, mDude.getX(), mDude.getY());
	    if(!mManager.hasEnded()){
	    	mManager.checkCollision(mDude);
	    	if(System.currentTimeMillis() > (mStartTime+50)){
	    		mTextField.setText("Beers: "+mManager.catchedBeers() + "   Time (ms): "+ Long.toString(System.currentTimeMillis()-mStartTime));
	    		mDude.go();
	    	}
	    }
    }
	
	private static int checkRange(int min, int max, int value){
	    return Math.min(max, Math.max(min, value));
	}
    
    public void SetPosition(int viewID, int leftMargin, int topMargin)
    {
    	View view = (View) findViewById(viewID);
        RelativeLayout.LayoutParams absParams = 
            (RelativeLayout.LayoutParams)view.getLayoutParams();
        absParams.leftMargin = leftMargin;
        absParams.topMargin = topMargin;
        view.setLayoutParams(absParams);
    }   
}